import numpy as np
from scipy.signal import find_peaks, argrelmax, argrelmin
import matplotlib.pyplot as plt

# 假设我们有一条曲线数据
x = np.linspace(0, 10, 100)
y = np.sin(x)

# 计算一阶导数
dy = np.gradient(y, x)

# 计算二阶导数
ddy = np.gradient(dy, x)

# 找到二阶导数的局部最大值和最小值，这些可能是拐点
peaks, _ = find_peaks(ddy)
troughs, _ = find_peaks(-ddy)

# 合并可能的拐点
inflection_points = np.sort(np.concatenate((peaks, troughs)))

# 验证拐点条件：一阶导数在拐点两侧应该变号
validated_inflection_points = []
for point in inflection_points:
    if point > 0 and point < len(dy) - 1:
        if np.sign(dy[point - 1]) != np.sign(dy[point + 1]):
            validated_inflection_points.append(point)

# 输出拐点的坐标
for point in validated_inflection_points:
    print(f"拐点坐标: ({x[point]}, {y[point]})")

# 可视化结果
plt.plot(x, y, label='Curve')
plt.plot(x[inflection_points], y[inflection_points], 'ro', label='Inflection Points (Unvalidated)')
plt.plot(x[validated_inflection_points], y[validated_inflection_points], 'go', label='Inflection Points (Validated)')
plt.legend()
plt.show()